#include<stdio.h>
#include<conio.h>
#include<string.h>

/*
Owais Lone
Roll no: 25133
Class: SE-II

String Operations
*/



#define size 100 //maximum size of strings.
//Custom Function Prototypes
void prompt(char a[],char b[]);
void printmenu(char a[]);
void strcpy_usr(char* b,char* a);
int strlen_usr(char a[]);
void strcat_usr(char a[],char b[]);
void strrev_usr(char a[]);
int strcmp_usr(char a[],char b[]);
int strstr_usr(char a[],char b[]);
int strpal(char a[]);



//Main Program
void main()
{
char a[size]="Welcome",b[size]="Hello",c[size]="Hi....",clip[size]="Clipboard",n[size],level[size];
int ch,ch1,sub_ch,cmp,i;
//aa=&a[0];
//bb=&b[0];
clrscr();
printf("Whats your name?\n");
printf("user@system:>");
flushall();
gets(n);
clrscr();
do
{
clrscr();
strcpy(level,"Main Menu");
printmenu(level);//Print Menu Level on Top
printf("\t\t## 1:Using Library Functions\t\t\b\b##\n");
printf("\t\t## 2:Using Custom Functions \t\t\b\b##\n");
printf("\t\t## 3:Let Me Go...Please\t\t\t\b\b##\n");
printmenu(n);//Print Username on Bottom
prompt(n,level);//Display Shell-Prompt
scanf("%d",&ch);
switch(ch)
{
case 1://String Operation By Using Library Functions
clrscr();
strcpy(level,"Library Functions");
printmenu(level);//Print Menu Level on Top
printf("\t\t## 1:Check Strings\t\t\t\b\b##\n");
printf("\t\t## 2:Copy\t\t\t\t\b\b##\n");
printf("\t\t## 3:Find Length \t\t\t\b\b##\n");
printf("\t\t## 4:Concatinate\t\t\t\b\b##\n");
printf("\t\t## 5:Reverse\t\t\t\t\b\b##\n");
printf("\t\t## 6:Compare \t\t\t\t\b\b##\n");
printf("\t\t## 7:Search\t\t\t\t\b\b##\n");
printf("\t\t## 8:Pallindrome\t\t\t\b\b##\n");
printf("\t\t## 9:Back to Main Menu\t\t\t\b\b##\n");
printmenu(n);//Print Username on Bottom
prompt(n,level);//Display Shell-Prompt
scanf("%d",&sub_ch);
	switch(sub_ch)
	{

	case 1: //Check
	clrscr();
	printf("First String is %s\nSecond String is %s\nThird String is %s",a,b,c);
	getch();
	break;
	case 2: //Copy
	clrscr();
	strcpy(level,"Copy");
	printmenu(level);
	printf("Copy\n1:First String\n2:Second String\n3:Third String\n");
	prompt(n,level);
	flushall();
	scanf("%d",&ch1);
	    switch(ch1)
	    {
	    case 1:
	    strcpy(clip,a);
	    printf("Copied String no 1");
	    break;
	    case 2:
	    strcpy(clip,b);
	    printf("Copied String no 2");
	    break;
	    case 3:
	    strcpy(clip,c);
	    printf("Copied String no 2");
	    break;
	    default:
	    printf("No string was copied");
	    }
	    clrscr();
	strcpy(level,"Copy");
	printmenu(level);
	prompt(n,level);
	printf("Paste into\n1: First String\n2: Second String\n3: Third String\n");
	prompt(n,level);
	scanf("%d",&ch1);
	    switch(ch1)
	    {
	    case 1:
	    strcpy(a,clip);
	    printf("Pasted in First String");
	    break;
	    case 2:
	    strcpy(b,clip);
	    printf("Pasted in Second String");
	    break;
	    case 3:
	    strcpy(c,clip);
	    printf("Pasted in Third String");
	    break;
	    }
	printf("\nYou can Check strings at the menu\n");
	getch();
	break;
	case 3: //Length
	clrscr();
	strcpy(level,"Length");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printf("%s is a %d characters long sentence",a,strlen(a));
	getch();
	break;
	case 4: //Concatinate
	clrscr();
	strcpy(level,"Concatinate");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	printf("Enter another sentence.(To be appended to First Sentence)\n");
	prompt(n,level);
	flushall();
	gets(b);
	strcat(a,b);
	printf("Result after Concatination is: %s",a);
	getch();
	break;
	case 5: //Reverse
	clrscr();
	strcpy(level,"Reverse");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	strrev(a);
	printf("Reverse of Input is %s",a);
	getch();
	break;
	case 6: //Compare
	clrscr();
	strcpy(level,"Compare");
	printmenu(level);
	printf("Enter First Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	printf("Enter Second Sentence\n");
	prompt(n,level);
	flushall();
	gets(b);
	clrscr();
	printmenu(level);
	if(strcmp(a,b)>0)
	printf("'%s' is greater than '%s'\n",a,b);
	else if(strcmp(a,b)<0)
	printf("'%s' is smaller than '%s'\n",a,b);
	else
	printf("'%s' & '%s' are same\n",a,b);
	getch();
	clrscr();
	break;
	case 7: //Search
	clrscr();
	strcpy(level,"Search");
	printmenu(level);
	printf("Enter First Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	printf("Search for\n");
	prompt(n,level);
	flushall();
	gets(b);
	clrscr();
	printmenu(level);
	if(strstr(a,b))
	printf("'%s' contains '%s'\n",a,b);
	else
	printf("'%s' doesn't contain '%s'\n",a,b);
	getch();
	clrscr();
	break;
	case 8://Palindrome
	clrscr();
	strcpy(level,"Palindrome");
	printmenu(level);
	printf("Enter a Word\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	strcpy(b,a);
	strrev(a);
	if(!strcmp(a,b))
	printf("%s is Palindrome\n",b);
	else
	printf("%s is not Palindrome\n",b);
	getch();
	clrscr();
	break;


	}//End Case for library functions
break;
case 2://String Operation By Using Custom Functions
clrscr();
strcpy(level,"User Defined Fuctions");
printmenu(level);//Print Menu Level on Top
printf("\t\t## 1:Check Strings\t\t\t\b\b##\n");
printf("\t\t## 2:Copy\t\t\t\t\b\b##\n");
printf("\t\t## 3:Find Length \t\t\t\b\b##\n");
printf("\t\t## 4:Concatinate\t\t\t\b\b##\n");
printf("\t\t## 5:Reverse\t\t\t\t\b\b##\n");
printf("\t\t## 6:Compare \t\t\t\t\b\b##\n");
printf("\t\t## 7:Search\t\t\t\t\b\b##\n");
printf("\t\t## 8:Pallindrome\t\t\t\b\b##\n");
printf("\t\t## 9:Back to Main Menu\t\t\t\b\b##\n");
printmenu(n);//Print Username on Bottom
prompt(n,level);//Display Shell-Prompt
scanf("%d",&sub_ch);
	switch(sub_ch)
	{
	case 1://Display Strings
	clrscr();
	printf("First String is\t %s\nSecond String is %s\nThird String is\t %s",a,b,c);
	getch();
	break;
	case 2: //Copy
	clrscr();
	strcpy_usr(level,"Copy");
	printmenu(level);
	printf("Copy\n1:First String\n2:Second String\n3:Third String\n");
	prompt(n,level);
	flushall();
	scanf("%d",&ch1);
	    switch(ch1)
	    {
	    case 1:
	    strcpy_usr(clip,a);
	    printf("Copied String no 1\n");
	    break;
	    case 2:
	    strcpy_usr(clip,b);
	    printf("Copied String no 2\n");
	    break;
	    case 3:
	    strcpy_usr(clip,c);
	    printf("Copied String no 2\n");
	    break;
	    default:
	    printf("No string was copied\n");
	    }
	    clrscr();
	strcpy_usr(level,"Copy");
	printmenu(level);
	prompt(n,level);
	printf("Paste into\n1: First String\n2: Second String\n3: Third String\n");
	prompt(n,level);
	scanf("%d",&ch1);
	    switch(ch1)
	    {
	    case 1:
	    strcpy_usr(a,clip);
	    printf("Pasted in First String\n");
	    break;
	    case 2:
	    strcpy_usr(b,clip);
	    printf("Pasted in Second String\n");
	    break;
	    case 3:
	    strcpy_usr(c,clip);
	    printf("Pasted in Third String\n");
	    break;
	    }
	printf("\nYou can Check strings at the menu\n");
	getch();
	break;
	case 3: //Length
	clrscr();
	strcpy_usr(level,"Length");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printf("%s is a %d characters long sentence\n",a,strlen_usr(a));
	getch();
	clrscr();
	break;
	case 4: //Concatinate
	clrscr();
	strcpy(level,"Concatinate");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	printf("Enter another sentence.(To be appended to First Sentence)\n");
	prompt(n,level);
	flushall();
	gets(b);
	strcat_usr(a,b);
	printf("Result after Concatination is: %s",a);
	getch();
	break;
	case 5: //Reverse
	clrscr();
	strcpy(level,"Reverse");
	printmenu(level);
	printf("Enter a Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	strrev_usr(a);
	printf("Reverse of Input is %s",a);
	getch();
	break;
	case 6: //Compare
	clrscr();
	strcpy(level,"Compare");
	printmenu(level);
	printf("Enter First Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	printf("Enter Second Sentence\n");
	prompt(n,level);
	flushall();
	gets(b);
	clrscr();
	printmenu(level);
	cmp=strcmp_usr(a,b);
	if(cmp==1)
	printf("'%s' is greater than '%s'\n",a,b);
	else if(cmp==2)
	printf("'%s' is smaller than '%s'\n",a,b);
	else
	printf("'%s' & '%s' are same\n",a,b);
	getch();
	clrscr();
	break;
	case 7: //Search
	clrscr();
	strcpy(level,"Search");
	printmenu(level);
	printf("Enter First Sentence\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	printf("Search for\n");
	prompt(n,level);
	flushall();
	gets(b);
	clrscr();
	printmenu(level);
	if(strstr_usr(a,b))
	printf("Just found %s in %s",b,a);

	else
	printf("Couldn't find %s in %s",b,a);

	getch();

	break;
	case 8://Pallindrome
	clrscr();
	strcpy(level,"Pallindrome");
	printmenu(level);
	printf("Enter a Word\n");
	prompt(n,level);
	flushall();
	gets(a);
	clrscr();
	printmenu(level);
	strpal(a);
	getch();
	clrscr();
	break;
	}
break;
case 3://Exit Case
clrscr();
printf("Exiting.");
for(i=0;i<20;i++)
{printf(".");
delay(50);}
break;
default:
printf("People who don't make mistakes, make nothing...\nTry Again\b");
getch();
clrscr();
}
}while(ch!=3);


clrscr();
}




//Custom Functions

void strcpy_usr(char* b,char* a)//Copy B to A
{
int i,j;
for(i=0;a[i]!='\0';i++)//copy A to B
{
b[i]=a[i];
}
b[i]='\0';//Terminate B
}

int strlen_usr(char a[size])//Calculate Length of a String
{
int i;
for(i=0;a[i]!='\0';i++);
return i;
}
void strcat_usr(char a[size],char b[size])//Concatinate
{
int i,j;
for(i=0;a[i]!='\0';i++);
for(j=0;b[j]!='\0';i++,j++)
a[i]=b[j];
}

void strrev_usr(char a[size])//Reverse
{
int i,j;
char b[size];
strcpy_usr(b,a);
i=strlen_usr(a);
i--;
for(j=0;i>=0;i--,j++)
a[j]=b[i];
a[j]='\0';
}

int strcmp_usr(char a[size],char b[size])//Compare
{
int i,flag;
for(i=0;a[i]!='\0'&&b[i]!='\0';)
{
    if(a[i]==b[i])
    {
    flag=0;
    i++;
    }
    else if(a[i]>b[i])
    {
    flag=1;
    break;
    }
    else
    {
    flag=2;
    break;
    }
}
return flag;
/*
int i,as=0,bs=0;
for(i=0;a[i]!='\0';i++)
as=as+a[i];
for(i=0;b[i]!='\0';i++)
bs=bs+b[i];

if(as>bs)
return(0);
else if(as<bs)
return(1);
else
return(2);
*/
}

int strstr_usr(char a[size],char b[size]) //Search
{
int i,j,z;
z=strlen(b);
for(i=0,j=0;a[i]!='\0';)
{
	   if(a[i]==b[j])
	   {
	   j++;
	   i++;
	   }
	   else
	   i++;
}

if(j==z)
return(1);
else
return(0);
}


int strpal(char a[size])//Pallindrome
{
char b[size];
	strcpy_usr(b,a);
	strrev_usr(a);
	if(!strcmp_usr(a,b))
	printf("%s is Palindrome\n",b);
	else
	printf("%s is not Palindrome\n",b);

}


void prompt(char a[],char b[])//Display Shell Prompt
{
printf("%s@%s:>",a,b);
}

void printmenu(char a[])//Display Menu
{
int s,r,k,i;
s=strlen(a);
k=s%2;
r=38-s;
r=r/2;
printf("\n\t\t");
for(i=0;i<=r;i++)
printf("*");
printf("%s",a);
for(i=0;i<=r;i++)
printf("*");
if(k!=0)
printf("*");
printf("\n");
}